#include "Com.h"
#include "queue.h"

/*************** DEFINES *********************/
#define COM_TX_QUEUELENGTH    20




/*************** GLOBAL VARIABLES ************/
xQueueHandle Com_TxMessageQueue;
static uint8 Com_Tx_IniFlag = 0;



/*************** CODE ************************/

void Com_Tx_Ini(void)
{
	Com_TxMessageQueue = xQueueCreate(COM_TX_QUEUELENGTH, sizeof(Com_Message_t));
	
	Com_Tx_IniFlag = 1;
}



ret_t Com_Tx_SendMessage(uint8 id, uint8 len, uint8* data)
{
	Com_Message_t msg;
	uint8 i;
	ret_t ret = RET_OK;
	
	// TODO: check id validity here
	if(   (len <= COM_MSGLENGTH) //check length
	    &&(Com_Tx_IniFlag == 1))    //check if initialized
	{	
		msg.len = len;
		msg.id = id;
	
		for(i=0;i<len;i++)
		{
			msg.data[i] = data[i];
		}
		
		//send message in the queue
		if(xQueueSend(Com_TxMessageQueue, &msg, 0 /*no block time*/) != pdPASS)
		{
			ret = RET_NOK;
		}
	}
	else
	{
		ret = RET_NOK;
	}
	
	return ret;
}


